#include "widget.h"
#include "ui_widget.h"
#include <QString>
#include <QTimer>
#include <QDebug>
#include <QPushButton>

Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    // 启动定时器
    timeId = startTimer(50);

    QTimer * timer = new QTimer(this);
    timer->start(500);
    connect(timer, &QTimer::timeout, [=](){
        static int num = 1;
        ui->label_2->setText(QString::number(num++));
    });
    connect(ui->pushButton, &QPushButton::clicked, [=](){
        timer->stop();
    });
    // 安装事件过滤器
    ui->label->installEventFilter(this);
    // 重写
}

Widget::~Widget()
{
    delete ui;
}
bool Widget::eventFilter(QObject *obj, QEvent *e)
{
    if(obj == ui->label)
    {
        if(e->type() == QEvent::MouseMove)
        {
//            qDebug() << "事件过滤器";
            return true;
        }
    }
    return QWidget::eventFilter(obj, e);
}
void Widget::timerEvent(QTimerEvent *ev)
{
    static int num = 1;
    ui->label->setText(QString::number(num++) + QString::number(ev->timerId()));
}
